/**
 * 订单单元测试
 * (1)查询订单数据
 * (2)下单
 * (3)结算
 * (4)生成预支付信息
 * (5)支付完成更新订单状态
 * (6)设置订单消息发送状态
 * (7)更新订单完成状态
 */
import { Guid } from "../common";

let url = 'http://localhost';
let port = '3000';
let apiUrl = url + ':' + port;
let should = require('chai').should(),
    expect = require('chai').expect,
    supertest = require('supertest'),
    api = supertest(apiUrl);

let orderNo = Guid.GetGuid();

/**
 * 查询订单数据
 * @param  {} orderId 订单Id
 */
describe('order', () => {
    it('should be an object with keys and values', (done) => {
        api.get('/order/getOrderById/8a7b248c65924c878adc456fa4c4cff0')
            .set('Accept', 'application/json')
            .expect(200)
            .end((err, res) => {
                let order = res.body.data;
                expect(order).to.have.property('id');
                expect(order).to.have.property('orderNo');
                expect(order.cart).to.not.equal(null);
                expect(order).to.have.property('createTime');
                expect(order.creator).to.not.equal(null);
                expect(order).to.have.property('orderType');
                expect(order).to.have.property('openid');
                done();
            })
    })
})

/**
 * 快餐版下单
 * @param  {} 订单对象
 */
describe('fastOrdering', () => {
    it('should be updated with a new name', (done) => {
        api.post('/order/fastOrdering')
            .set('Accept', 'application/json')
            .send({
                "id": orderNo,
                "orderNo": orderNo,
                "creator": { "id": "weixin", "name": "wx" },
                "cart": {
                    "items": [
                        {
                            "id": "911QHJPC4WU47U6OXY1MJDHV9",
                            "price": 28,
                            "inventoryId": "8tenakprro08k84ks4kkgs404",
                            "inventoryName": "大刀耳片",
                            "qty": 1,
                            "extended": {},
                            "amount": 28
                        }
                    ]
                },
                "storeId": "1doq6jxo4tdwosgk0s8og0408",
                "terminalName": "wexin",
                "sourceid": "mpj/msite/wx-fast",
                "serialNo": "wx-28",
                "openid": "sdfsfsdfsfdsdfsdfsd2324"
            })
            .expect(200)
            .end((err, res) => {
                let order = res.body.data;
                expect(order).to.have.property('id');
                expect(order).to.have.property('orderNo');
                expect(order.cart).to.not.equal(null);
                expect(order).to.have.property('createTime');
                expect(order.creator).to.not.equal(null);
                expect(order).to.have.property('orderType');
                expect(order).to.have.property('openid');
                done();
            })
    })
})

/**
 * 结算
 * @param  {} 订单对象
 */
describe('fastOrdering', () => {
    it('should be updated with a new name', (done) => {
        api.post('/order/fastOrdering')
            .set('Accept', 'application/json')
            .send({
                "orderNo": orderNo,
                "status": "ordered"
            })
            .expect(200)
            .end((err, res) => {
                let order = res.body.data;
                expect(order).to.have.property('id');
                expect(order).to.have.property('orderNo');
                expect(order.cart).to.not.equal(null);
                expect(order).to.have.property('checkout');
                expect(order).to.have.property('createTime');
                expect(order.creator).to.not.equal(null);
                expect(order).to.have.property('orderType');
                expect(order).to.have.property('openid');
                done();
            })
    })
})

/**
 * 生成预支付信息
 * @param  {} 订单对象
 */
describe('generatePay', () => {
    it('should be updated with a new name', (done) => {
        api.post('/order/generatePay')
            .set('Accept', 'application/json')
            .send({
                "orderNo": orderNo,
                "channel": "wx",
                "channelId": "wx-id"
            })
            .expect(200)
            .end((err, res) => {
                let order = res.body.data;
                expect(order).to.have.property('id');
                expect(order).to.have.property('orderNo');
                expect(order.cart).to.not.equal(null);
                expect(order).to.have.property('checkout');
                expect(order.checkout).to.have.property('payments');
                expect(order).to.have.property('createTime');
                expect(order.creator).to.not.equal(null);
                expect(order).to.have.property('orderType');
                expect(order).to.have.property('openid');
                done();
            })
    })
})

/**
 * 支付完成更新订单状态
 * @param  {} 订单对象
 */
describe('completePay', () => {
    it('should be updated with a new name', (done) => {
        api.post('/order/completePay')
            .set('Accept', 'application/json')
            .send({
                "amountDue": 28,
                "orderNo": orderNo,
                "payments": [
                    {
                        "id": "CCAAABBBOYM8KJ7XQ6LHZT7YZ",
                        "channel": "wechat-pay",
                        "channelId": "wechat-pay",
                        "amount": 28,
                        "storeId": "1doq6jxo4tdwosgk0s8og0408",
                        "received": 28,
                        "payMode": "pay",
                        "createTime": 1472007280062,
                        "extended": { "trade_type": "JSAPI" }
                    }
                ]
            })
            .expect(200)
            .end((err, res) => {
                let order = res.body.data;
                expect(order).to.have.property('id');
                expect(order).to.have.property('orderNo');
                expect(order.cart).to.not.equal(null);
                expect(order).to.have.property('checkout');
                expect(order.checkout).to.have.property('payments');
                expect(order).to.have.property('createTime');
                expect(order.creator).to.not.equal(null);
                expect(order).to.have.property('orderType');
                expect(order).to.have.property('openid');
                done();
            })
    })
})

/**
 * 设置订单消息发送状态
 * @param  {} 订单对象
 */
describe('addProcessAction', () => {
    it('should be updated with a new name', (done) => {
        api.post('/order/addProcessAction')
            .set('Accept', 'application/json')
            .send({
                "orderNo": orderNo,
                "statusType": "weixinorder"
            })
            .expect(200)
            .end((err, res) => {
                let order = res.body.data;
                expect(order).to.have.property('id');
                expect(order).to.have.property('orderNo');
                expect(order.cart).to.not.equal(null);
                expect(order).to.have.property('checkout');
                expect(order.checkout).to.have.property('payments');
                expect(order).to.have.property('createTime');
                expect(order.creator).to.not.equal(null);
                expect(order).to.have.property('orderType');
                expect(order).to.have.property('openid');
                done();
            })
    })
})

/**
 * 更新订单完成状态
 * @param  {} 订单对象
 */
describe('closeOrder', () => {
    it('should be updated with a new name', (done) => {
        api.post('/order/closeOrder')
            .set('Accept', 'application/json')
            .send({
                "orderNo": orderNo,
                "lastPrintTime": new Date().getTime(),
                "processStatus": "received"
            })
            .expect(200)
            .end((err, res) => {
                let order = res.body.data;
                expect(order).to.have.property('id');
                expect(order).to.have.property('orderNo');
                expect(order.cart).to.not.equal(null);
                expect(order).to.have.property('checkout');
                expect(order.checkout).to.have.property('payments');
                expect(order).to.have.property('createTime');
                expect(order.creator).to.not.equal(null);
                expect(order).to.have.property('orderType');
                expect(order).to.have.property('openid');
                done();
            })
    })
})